package com.put.mybills.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	
	 public static final int DATABASE_VERSION = 1;	 
	 public static final String DATABASE_NAME = "factureManager";	 
	 public static final String FACTURE_TABLE_NAME = "facture";	
	 public static final String FACTURE_ID = "id";
	 public static final String FACTURE_NAME = "name";
	 public static final String FACTURE_TERM = "data";
	 public static final String FACTURE_INTERVAL = "interval";
	 public static final String FACTURE_DESCRIPTION = "description";
	 public static final String FACTURE_ICON = "icon";
	 public static final String FACTURE_EMAIL_NOTYFICATION = "email";
	 public static final String FACTURE_POPUP_NOTYFICATION = "popup";
	 public static final String FACTURE_ICON_DRIVE_ID = "drive_id";
	 public static final String FACTURE_TEMPLAT_BARCODE_ID = "template";
	 
	 private static final String FACTURE_TABLE_CREATE = 
			"CREATE TABLE "   + FACTURE_TABLE_NAME + "(" + 
			FACTURE_ID     + " integer primary key autoincrement, " + 			 		
			FACTURE_NAME  + " text not null UNIQUE," +		
			FACTURE_INTERVAL  + " integer," + 
			FACTURE_DESCRIPTION  + " text," + 	
			FACTURE_ICON  + " TEXT," + 	
			FACTURE_TERM + " TEXT," +
			FACTURE_EMAIL_NOTYFICATION + " integer," +
			FACTURE_POPUP_NOTYFICATION + " integer," +
			FACTURE_ICON_DRIVE_ID + " TEXT," +
			FACTURE_TEMPLAT_BARCODE_ID + " integer," +
			"FOREIGN KEY(template) REFERENCES barcode(id));";
			

	 
	 public static final String BILL_TABLE_NAME = "bill";	 
	 public static final String BILL_ID = "id";
	 public static final String BILL_FACTUREID = "facture_id";
	 public static final String BILL_NAME = "name";
	 public static final String BILL_TERM = "term";
	 public static final String BILL_PRICE = "price";
	 public static final String BILL_DESCRIPTION = "description";
	 public static final String BILL_PICTURE_PATH = "path";	 
	 public static final String BILL_IMAGE_DRIVE_ID = "drive_id";
	 public static final String BILL_IFPAID = "paid";

	 	 
	 private static final String BILL_TABLE_CREATE =
			 "CREATE TABLE " + BILL_TABLE_NAME + " (" +	 
    		 BILL_ID + " integer primary key autoincrement," +
    		 BILL_FACTUREID + " integer, " +
    		 BILL_NAME + " text UNIQUE, " +
    		 BILL_PRICE + " real not null, " +
    		 BILL_DESCRIPTION + " TEXT, " +   
    		 BILL_TERM + " TEXT, " + 
    		 BILL_PICTURE_PATH + " TEXT," +
    		 BILL_IMAGE_DRIVE_ID + " TEXT UNIQUE, " +
    		 BILL_IFPAID + " integer not null, " +
    		 "FOREIGN KEY(facture_id) REFERENCES facture(id) );";
	 
	 
	 public static final String UNIT_TABLE_NAME = "units";
	 public static final String UNIT_ID = "id";
	 public static final String UNIT_FACTURE_ID = "facture_id";
	 public static final String UNIT_BILL_ID = "bill_id";
	 public static final String UNIT_NAME= "name";
	 public static final String UNIT_METRIC= "metric";
	 public static final String UNIT_PRICE= "price";
	 public static final String UNIT_VALUE= "value";
	 
	 
	 private static final String UNIT_TABLE_CREATE = 
			 "CREATE TABLE " + UNIT_TABLE_NAME + " (" +	 
    		 UNIT_ID + " integer primary key autoincrement," +
    		 UNIT_FACTURE_ID + " integer, " +
    		 UNIT_BILL_ID + " integer, " + 
    		 UNIT_NAME + " text, " +
    		 UNIT_METRIC + " text, " +
    		 UNIT_PRICE + " real, " +
    		 UNIT_VALUE + " real );";
	 
	 
	 public static final String DATA_TABLE_NAME = "app_data";
	 public static final String DATA_NAME = "name";	
	 public static final String DATA_DRIVE= "driveid";	 
	 public static final String DATA_BILL = "bill";
	 public static final String DATA_FACTURE = "facture";
	 public static final String DATA_UNITS = "unit";
	 
	 
	 private static final String DATA_TABLE_CREATE = 
			 "CREATE TABLE " + DATA_TABLE_NAME + " (" +	
			 DATA_NAME  + " text UNIQUE," +		
			 DATA_DRIVE + " text);";
	 
	 
	 public static final String TEMPLATE_BARCODE_TABLE_NAME = "barcode";
	 public static final String TEMPLATE_BARCODE_ID = "id";
	 public static final String TEMPLATE_BARCODE_NAME = "name";
	 public static final String TEMPLATE_BARCODE_PREFIX = "prefix";	
	 public static final String TEMPLATE_BARCODE_SUFFIX= "suffix";	 

	 
	 
	 private static final String TEMPLATE_BARCODE_TABLE_CREATE = 
			 "CREATE TABLE " + TEMPLATE_BARCODE_TABLE_NAME + " (" +	
			 TEMPLATE_BARCODE_ID + " integer primary key autoincrement," +
			 TEMPLATE_BARCODE_NAME + " text UNIQUE, " +
			 TEMPLATE_BARCODE_PREFIX  + " integer ," +					
			 TEMPLATE_BARCODE_SUFFIX + " integer);";
	
	 

	DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
	
	
	@Override
	public void onCreate(SQLiteDatabase db) {	
		db.execSQL(TEMPLATE_BARCODE_TABLE_CREATE);
		db.execSQL(FACTURE_TABLE_CREATE);
		db.execSQL(BILL_TABLE_CREATE);
		db.execSQL(UNIT_TABLE_CREATE);
		db.execSQL(DATA_TABLE_CREATE);
		
	}
	


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//db.execSQL(DROP_BILL_TABLE);
		//db.execSQL(DROP_FACTURE_TABLE);
		//onCreate(db);
		
	}

}
